//
// Created by TheMorbidArk on 2022/11/6.
//

#ifndef VANTIDEL_CPP_OBJTHREAD_H
#define VANTIDEL_CPP_OBJTHREAD_H

#include "ObjFn.h"

class ObjThread {
public:
    ObjHeader objHeader;

    Value *stack = nullptr;  //运行时栈的栈底
    Value *esp = nullptr;    //运行时栈的栈顶
    uint32_t stackCapacity = 0;  //栈容量

    Frame *frames = nullptr;   //调用框架
    uint32_t usedFrameNum = 0;   //已使用的frame数量
    uint32_t frameCapacity = 0;  //frame容量

    //"打开的upvalue"的链表首结点
    ObjUpvalue *openUpvalues = nullptr;

    //当前thread的调用者
    ObjThread *caller = nullptr;

    //导致运行时错误的对象会放在此处,否则为空
    Value errorObj{};

    ObjThread(VM *vm, ObjClosure *objClosure);

    static void PrepareFrame(ObjThread *objThread, ObjClosure *objClosure, Value *stackStart);

    static ObjThread *NewObjThread(VM *vm, ObjClosure *objClosure);

    static void ResetThread(ObjThread *objThread, ObjClosure *objClosure);

};


#endif //VANTIDEL_CPP_OBJTHREAD_H
